#import "ToDoPreferenceView.h"

@implementation ToDoPreferenceView

- (id)initWithFrame:(struct CGRect)frame
{
	self = [super initWithFrame:frame];
	
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(reloadTableData)
												 name:@"MMCurrentTransactionDidChangeNotification"
											   object:nil];
	
  toDoManager = [ToDoManager sharedToDoManager];
	td = [MobileToDoListApplication sharedInstance];
	
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	rowHeights = [[NSMutableArray alloc] init];
	[rowHeights addObject:@"30"];
	[rowHeights addObject:@"40"];
	[rowHeights addObject:@"75"];
	[rowHeights addObject:@"100"];
	
	navBar = [[UINavigationBar alloc] init];
	[navBar setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f)];
	[navBar showButtonsWithLeftTitle:@"Back" rightTitle:nil leftBack:YES];
	[navBar setDelegate:self];
	[navBar enableAnimation];
	
	
	table = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(0.0f, 45.0f, 320.0f, 415.0f)];
	[table setDataSource:self];
	[table setDelegate:self];
	[table reloadData];
	[table setKeyboardVisible:NO];
	
	[self addSubview:navBar];
	[self addSubview:table];

	
	
	return self;
	
}

- (void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button
{
	if([ rowHeight selectedSegment ] >= 0) {
		[toDoManager setPref:@"rowHeight" withValue:[rowHeights objectAtIndex:[ rowHeight selectedSegment ] ]];
	}


	[toDoManager writePreferences];
	
	//deselect row
	[table selectRow: 0 byExtendingSelection: NO withFade: YES];
	
	[td showToDosWithTransition:2];
}

- (void)reloadTableData
{
	[table setKeyboardVisible:NO];
	[table reloadData];
}

- (void)tableRowSelected:(NSNotification *)notification
{

	
	if([table selectedRow] == 3) {
		[table selectRow: 0 byExtendingSelection: NO withFade: YES];
		[td showToDoGroupsEditWithTransition:1];
	}
	if([table selectedRow] == 4) {
		[table selectRow: 0 byExtendingSelection: NO withFade: YES];
		[td showToDoOrderByWithTransition:1];
	}
	if([table selectedRow] == 5) {
		[table selectRow: 0 byExtendingSelection: NO withFade: YES];
		[td showToDoDeleteAfterWithTransition:1];
	}
	if([table selectedRow] == 7) {
		[table selectRow: 0 byExtendingSelection: NO withFade: YES];
		[td showBetaSheet];
	}
	if([table selectedRow] == 8) {
		[table selectRow: 0 byExtendingSelection: NO withFade: YES];
		[td showChangesSheet];
	}
	if([table selectedRow] == 9) {
		[table selectRow: 0 byExtendingSelection: NO withFade: YES];
		[td showNextFuturesSheet];
	}
	if([table selectedRow] == 10) {
		[table selectRow: 0 byExtendingSelection: NO withFade: YES];
		[td openURL:[NSURL URLWithString:@"http://www.include7.ch/mobiletodolist"]];
	}
	
}

- (BOOL) table:(UITable *) table canDeleteRow:(int) row {
	return YES;
}

- (BOOL) table:(UITable *) table canMoveRow:(int) row {
	return YES;
}

#pragma mark ----------Datasource Methods-----------

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table
{
	return 3;
}

- (int)preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group
{
	if(group == 0) {
		return 1;	
	}
	if(group == 1) {
		return 3;
	}
	if(group == 2) {
		return 4;
	}
	
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForGroup:(int)group
{
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	
	return [cell autorelease];
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group
{
	if (group == 0)
	{
		if (row == 0)
		{
			UIPreferencesTextTableCell *cell;
			cell = [[UIPreferencesTextTableCell alloc] init];
			rowHeight = [[UISegmentedControl alloc]
	        initWithFrame:CGRectMake(130.0f, 6.0f, 170.0f, 55.0f)];
		    [ rowHeight insertSegment:0 withTitle:[rowHeights objectAtIndex:0] animated: NO ];
		    [ rowHeight insertSegment:1 withTitle:[rowHeights objectAtIndex:1] animated: NO ];
		    [ rowHeight insertSegment:2 withTitle:[rowHeights objectAtIndex:2] animated: NO ];
		  	[ rowHeight insertSegment:3 withTitle:[rowHeights objectAtIndex:3] animated: NO ];
			int i;
			for(i = 0; i < [rowHeights count]; i++)
			{
				if([[rowHeights objectAtIndex:i] isEqualToString:[toDoManager getPref:@"rowHeight"]]) {
					[ rowHeight selectSegment:i ];
				}
			}
			[ cell setTitle:@"Row Height" ];
      		[ cell addSubview:rowHeight ];
			return [cell autorelease];
		}
		if (row == 1)
		{
			UIPreferencesTextTableCell *cell;
			cell = [[UIPreferencesTextTableCell alloc] init];
			[ cell setTitle:@"Auto-Save Game" ];
	        showPriorityControll = [[UISwitchControl alloc]
	        initWithFrame:CGRectMake(210.0f, 7.0f, 120.0f, 30.0f)];
	        [ showPriorityControll setValue: NO ];
			[ cell setTitle:@"Show Priority" ];
	        [ cell  addSubview:showPriorityControll ];
			return [cell autorelease];
		}
		
		
	}

	if (group == 1)
	{
		if (row == 0)
		{
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];

			cell = [[[UIPreferencesTableCell alloc] init] retain];
			//[[cell textField] setDelegate:self];
			[cell setTitle:@"Manage Groups"];
			[cell setAlignment:1];
			[cell setShowDisclosure:YES];
			return [cell autorelease];
		}
		if (row == 1)
		{
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];

			cell = [[[UIPreferencesTableCell alloc] init] retain];
			//[[cell textField] setDelegate:self];
			[cell setTitle:@"Order By..."];
			[cell setAlignment:1];
			[cell setShowDisclosure:YES];
			return [cell autorelease];
		}
		if (row == 2)
		{
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];

			cell = [[[UIPreferencesTableCell alloc] init] retain];
			//[[cell textField] setDelegate:self];
			[cell setTitle:@"Delete Checked ToDos After..."];
			[cell setAlignment:1];
			[cell setShowDisclosure:YES];
			return [cell autorelease];
		}
	}
	if (group == 2)	{
		if (row == 0)
		{
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];

			cell = [[[UIPreferencesTableCell alloc] init] retain];
			//[[cell textField] setDelegate:self];
			[cell setTitle:@"About"];
			[cell setAlignment:1];
			return [cell autorelease];
		}
		if (row == 1)
		{
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];

			cell = [[[UIPreferencesTableCell alloc] init] retain];
			//[[cell textField] setDelegate:self];
			[cell setTitle:@"Changes To Version 0.30"];
			[cell setAlignment:1];
			return [cell autorelease];
		}
		if (row == 2)
		{
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];

			cell = [[[UIPreferencesTableCell alloc] init] retain];
			//[[cell textField] setDelegate:self];
			[cell setTitle:@"Planed Functions For V0.5"];
			[cell setAlignment:1];
			return [cell autorelease];
		}
		if (row == 3)
		{
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];

			cell = [[[UIPreferencesTableCell alloc] init] retain];
			//[[cell textField] setDelegate:self];
			[cell setTitle:@"Donate"];
			[cell setAlignment:1];
			return [cell autorelease];
		}
	}
}

- (float)preferencesTable:(UIPreferencesTable *)aTable 
    heightForRow:(int)row 
    inGroup:(int)group 
    withProposedHeight:(float)proposed 
{
    if (group == 0) {
        switch (row) {
            case 0 :
                return 55;
                break;
        }
    }
    return proposed;
}

@end